<template>
  <div v-loading="loading" class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
        <tree-tools :node-data="titleData" @on-open="toggleAddBox" />
        <!-- 树形菜单 -->
        <el-tree :data="departs" :props="defaultProps">
          <!-- 中间的代码就是插槽内容，用于定制每一行的布局效果 -->
          <!-- scope的数据是子组件通过slot标签的属性绑定提供的 :data = item -->
          <template v-slot="scope">
            <tree-tools :edit-dept="editFn" :node-data="scope.data" @on-success="onSuccess" @on-open="toggleAddBox" />
          </template>
        </el-tree>
      </el-card>
    </div>
    <!-- 添加部门 -->
    <!-- 在组件上定义的自定义事件中，$event表示子组件传递的数据，而不是事件对象 -->
    <!-- <AddDept :is-show-dept="isShowDept" @on-close="isShowDept=$event" /> -->
    <AddDept :operator-flag="operatorFlag" :dept-id="currentDeptId" :dlist="dlist" :is-show-dept.sync="isShowDept" @on-success="onSuccess" />
  </div>
</template>

<script>
import TreeTools from './components/TreeTools.vue'
import { reqGetDepartmentsAPI } from '@/api/department.js'
import { translateListToTreeData } from '@/utils'
import AddDept from './components/AddDept.vue'
export default {
  name: 'Departments',
  components: {
    TreeTools,
    AddDept
  },
  data() {
    return {
      // 页面加载提示
      loading: false,
      operatorFlag: '',
      // 当前部门id
      currentDeptId: '-1',
      // 控制添加部门弹窗的显示和隐藏
      isShowDept: false,
      dlist: [],
      departs: [
        {
          name: '总裁办',
          manager: '曹操',
          children: [{ name: '董事会', manager: '曹丕' }]
        },
        { name: '行政部', manager: '刘备' },
        { name: '人事部', manager: '孙权' }
      ],
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      // 标题的数据
      titleData: {
        name: '快乐前端程序员',
        manager: '负责人',
        root: true
      }
    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    editFn(id) {
      // 点击编辑按钮，显示编辑弹窗
      this.currentDeptId = id
      this.isShowDeptEdit = true
    },
    // 控制添加部门的弹窗的打开和关闭
    toggleAddBox (info) {
      this.currentDeptId = info.id
      this.operatorFlag = info.type
      console.log(this.operatorFlag)
      this.isShowDept = true
    },
    onSuccess () {
      this.loadDepartments()
    },
    // 获取组织架构列表
    async loadDepartments() {
      try {
        this.loading = true
        const res = await reqGetDepartmentsAPI()
        // console.log(res)
        if (res.code === 10000) {
          this.dlist = res.data.depts
          this.titleData.name = res.data.companyName
          this.departs = translateListToTreeData(res.data.depts, '')
        }
      } catch {
        // vue的$message方法
        this.$message.error('获取组织架构数据失败')
      } finally {
        this.loading = false
      }
    }

  }

  /* 默认的就是label对应label children对应children 时可省略
  当后台返回的数据不是的时候就需要 defaultProps属性了
  defaultProps: {
    children: 'children',
    label: 'name'
  }*/
}
</script>

<style scoped>
.tree-card {
  padding: 30px 30px;
  font-size:14px;
}
</style>
